package ronin.fx;

import java.util.*;
import java.awt.*;
import java.awt.geom.*;
import ronin.graphics.*;
import ronin.actor2d.*;

import ronin.fx.*;

public class ParticleSystem extends Object
{
	public static final int PS_SMOKE_TRAIL 	= 0;
	public static final int PS_EXPLOSION	= 1;
	public static final int PS_HALO_RING	= 2;
	public static final int PS_SMOKE_SWIRL	= 3;
	public static final int PS_BLUE_MYST	= 4;
	
	// system type
	int systemType;
	
	public ParticleManager manager;
	
	// state of system
	public boolean alive;
	
	// position of emmitter
	public Vector2D pos;
	
	// offset position used when attached to an object
	public Vector2D offsetPos;
	
	public Vector2D forces;
	
	// velocity of emitter
	// public Vector2D vel;
	
	// actor to attach system to
	public Actor2D host;
	
	// attached or not
	public boolean leach;
	
	// bounding box for emmiter
	public Rectangle2D bounds;
	
	// texture image used for painting
	public Image texture;
	
	// particle pool
	public LinkedList<Particle> particles;
	
	// used for objects with varying shapes
	public Shape shape;
	
	// number of live particles
	public int maxParticles;
	
	// blend mode for this emmiter
	public AlphaComposite alpha;
	
	// default initialization information
	public PSInitInfo defInitInfo;
	
	public boolean debug = false;
	
	public int lifespan;
	
	public boolean immortal; // if the particle system is immortal it runs for ever
	
	public ParticleSystem(Vector2D newPos, ParticleManager m)
	{
		particles = new LinkedList<Particle>();
		
		alive		 = true;
		
		manager	 	 = m;
		
		pos 	 	 = newPos;
		offsetPos	 = null;
		
		leach		 = false;
		host		 = null;
	}
	
	
	// new pos represents where on the actor the system is attached to
	public ParticleSystem(Actor2D objectAttachedTo, Vector2D newPos, ParticleManager m)
	{
		particles = new LinkedList<Particle>();
				
		alive		 = true;
		
		manager	 	 = m;

		leach	 	 = true;
		host		 = objectAttachedTo;
		
		offsetPos	 = newPos;
		setAttachedPos();

		

	}
	
	
	// sets the position of the actor to the offset position set for 
	// emmitters attached to actors
	public void setAttachedPos()
	{
		pos.setX( host.getPos().getX() + offsetPos.getX() );
		pos.setY( host.getPos().getY() + offsetPos.getY() );
	}
	
	
	public void setupSystem(PSInitInfo initInfo)
	{
		systemType   = initInfo.type;
		
		maxParticles = initInfo.maxParticles;
		
		shape 		 = initInfo.shape;
		
		if(initInfo.textureFile != "")
		{
			texture = manager.getImage(initInfo.textureFile);
		}
		
		alpha  = initInfo.alpha;
		
		bounds = initInfo.bounds;
		
		updateBounds();
	}
		
	public void update()
	{
		if(leach)
		{
			setAttachedPos();
			updateBounds();
		}
		
		// if particle pool empty kill system and return
		if(particles.size() == 0)
		{
			if(debug)System.out.println("PS dead");
			
			alive = false;
			return;
		}

	}
	
	public void updateBounds()
	{
		// remember pos is based on the center of object, 
		// so setup bounds accordingly
		
		int bw = (int) bounds.getWidth();
		int bh = (int) bounds.getHeight();
		
		bounds.setRect(pos.getX() - (bw/2), pos.getY() - (bh/2), bw, bh);
	}
	
	
	public void releaseParticle(int i)
	{
		manager.particlePool.add((Particle)particles.remove(i));
	}
	
	
	public void releaseParticles()
	{
		if(debug)System.out.println("Releasing " + particles.size() + "Particles");
		
		for(int i=particles.size()-1; i>=0; i--)
		{
			manager.particlePool.add((Particle)particles.remove(i));
		}
	}
	
	public void paint(Graphics2D g2d)
	{
		
	}
	
	
	public void startParticle(int i)
	{
		if(i <= 0) return; // i must be greater than 0
		
		Particle p;
		
		for(int n=0; n < i; n++)
		{
			p = new Particle();
			
		}
	}
}


